package one.stand.event;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.jmp.base.LabelApi;
import com.jmp.base.dto.SmsCodeEnums;
import com.jmp.base.dto.SmsSendRequest;
import com.jmp.feign.SmsFeign;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import one.stand.mapper.CompanyCustomerLableMapperExt;
import one.stand.mapper.CustomerLabelMapperExt;
import one.stand.model.BusinessCard;
import one.stand.model.Company;
import one.stand.model.CompanyCustomerLable;
import one.stand.model.CompanyLicense;
import one.stand.service.BusinessCardService;
import one.stand.service.CompanyBaseService;
import one.stand.service.CompanyLicenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.Objects;

@Component
@Async("asyncServiceExecutor")
@Slf4j
public class CompanyEventListener {
    @Autowired
    private SmsFeign smsFeign;

    @Autowired
    private BusinessCardService businessCardService;
    @Autowired
    private CompanyBaseService companyBaseService;
    @Autowired
    private CompanyLicenseService companyLicenseService;
    @Autowired
    private CustomerLabelMapperExt customerLabelMapperExt;
    @Autowired
    private CompanyCustomerLableMapperExt companyCustomerLableMapperExt;

    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void handle(CompanyLicenseAgreeEvent event) {
        log.info("event:{}", event.getSource());

        Company company = companyBaseService.getById(event.getCompanyId());
        BusinessCard card = businessCardService.getByUserId(company.getUserId());

        CompanyLicense companyLicense = companyLicenseService.getByCompanyId(event.getCompanyId());
        final boolean isJxs = companyLicense.getType() == 1;//1经销
        SmsParam smsParam = SmsParam.builder().company(company.getCompanyName()).build();
        smsFeign.sendBatchSms(SmsSendRequest.builder()
                .type(isJxs ? SmsCodeEnums.jxsSignSuccess.getCode() : SmsCodeEnums.jxsSignSuccessSupplier.getCode())
                .phones(Lists.newArrayList(card.getPhone()))
                .params(JSON.toJSONString(smsParam))
                .build());

        //标签
        if (isJxs && !companyBaseService.isByLabel(event.getCompanyId(), CompanyBaseService.LABEL_JXS)) {
            //企业权限标签
            CompanyCustomerLable label = companyCustomerLableMapperExt.selectLabel(CompanyBaseService.LABEL_JXS);
            if (Objects.nonNull(label)) {
                customerLabelMapperExt.insertByType(LabelApi.LABEL_TYPE_COMPANY, event.getCompanyId(), new Integer[]{label.getLableId()});
            }
        }
    }

    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void handle(CompanyLicenseRefuseEvent event) {
        log.info("event:{}", event.getSource());

        CompanyLicense companyLicense = companyLicenseService.getByCompanyId(event.getCompanyId());
        SmsParamFail smsParam = SmsParamFail.builder()
                .company(event.getCompanyName())
                .reason(event.getReason())
                .build();
        smsFeign.sendBatchSms(SmsSendRequest.builder()
                .type(companyLicense.getTypeInit() == 1 ? SmsCodeEnums.jxsSignFail.getCode() : SmsCodeEnums.jxsSignFailSupplier.getCode())//1经销
                .phones(Lists.newArrayList(event.getPhone()))
                .params(JSON.toJSONString(smsParam))
                .build());
    }

    @Data
    @Builder
    private static class SmsParam {
        private String company;
    }

    @Data
    @Builder
    private static class SmsParamFail {
        private String company;
        private String reason;
    }
}
